package com.example.smartcity.ui.dashboard;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smartcity.R;
import com.example.smartcity.ui.Bean.AddPaBean;
import com.example.smartcity.ui.Bean.stateBean;
import com.example.smartcity.ui.Tools.File;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.android.material.snackbar.Snackbar;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.Calendar;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

public class addPaient extends AppCompatActivity {

    private EditText ETName;
    private RadioButton RBMan;
    private EditText ETUserIDCard;
    private TextView TVDate;
    private EditText ETPhoneNumber;
    private EditText ETPosition;
    private TextView btnCanel;
    private TextView btnOk;
    private Calendar calendar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_paient);

        ETName = findViewById(R.id.ET_name);
        RBMan = findViewById(R.id.RB_man);
        ETUserIDCard = findViewById(R.id.ET_userIDCard);
        TVDate = findViewById(R.id.TV_date);
        ETPhoneNumber = findViewById(R.id.ET_phoneNumber);
        ETPosition = findViewById(R.id.ET_position);
        btnCanel = findViewById(R.id.btn_Canel);
        btnOk = findViewById(R.id.btn_ok);
        initView();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        calendar = Calendar.getInstance();
        //初始化日期
        TVDate.setText(calendar.get(Calendar.YEAR)+"-"+calendar.get(Calendar.MONTH)+"-"+calendar.get(Calendar.DAY_OF_MONTH));
        //修改日期
        TVDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(addPaient.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        TVDate.setText(year+"-"+month+"-"+dayOfMonth);
                    }
                },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
        //取消
        btnCanel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addPaient.this.finish();
            }
        });
        //确定
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                if (ETName.getText().length()>0&&ETUserIDCard.getText().length()==18&&ETPhoneNumber.length()==11&&ETPosition.length()>0){
                    AddPaBean bean = new AddPaBean();
                    //设置用户ID
                    bean.setUserId(Integer.valueOf(getSharedPreferences("selfMessage",MODE_PRIVATE).getString("userID",null)));
                    //设置姓名
                    bean.setName(ETName.getText().toString());
                    bean.setAdders(ETPosition.getText().toString());
                    bean.setBirthday(TVDate.getText().toString());
                    bean.setSex(RBMan.isChecked()?1:0);
                    bean.setCardId(ETUserIDCard.getText().toString());
                    bean.setTel(ETPhoneNumber.getText().toString());
                    String Json = gson.toJson(bean,AddPaBean.class);
                    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"),Json);
                    String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/userinfo/patient";
                    OkHttpUtils.getCall(newUrl,requestBody,new File(getBaseContext()).readFile("Token")).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Log.d("TAG", "onFailure: "+e.getMessage());
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            String Json = response.body().string();
                            stateBean stateBean = gson.fromJson(Json, com.example.smartcity.ui.Bean.stateBean.class);
                            Looper.prepare();
                            if (stateBean.getCode()==200){
                                Toast.makeText(addPaient.this, "添加成功", Toast.LENGTH_SHORT).show();
                                addPaient.this.finish();
                            }else{
                                Toast.makeText(addPaient.this, "添加失败", Toast.LENGTH_SHORT).show();
                            }
                            Looper.loop();
                        }
                    });

                }else{
                    Snackbar.make(v,"输入的信息不完整或不正确",Snackbar.LENGTH_SHORT).show();
                }
            }
        });

    }
}